<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="###Java 邮件(JavaMail)">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Java-邮件(JavaMail)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Java-Special-Function/Java-邮件(JavaMail)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="###Java 邮件(JavaMail)">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T10:39:50.189Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java-邮件(JavaMail)">
<meta name="twitter:description" content="###Java 邮件(JavaMail)">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Java-Special-Function/Java-邮件(JavaMail)/"/>





  <title>Java-邮件(JavaMail) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Java-Special-Function/Java-邮件(JavaMail)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Java-邮件(JavaMail)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>###Java 邮件(<a href="http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.7-oth-JPR" target="_blank" rel="noopener">JavaMail</a>)</p>
<a id="more"></a>
<ol>
<li><p>创建MailSenderInfo Bean，用来存储邮件使用的基本信息。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 1. 发送邮件需要使用的基本信息</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class MailSenderInfo &#123;</span><br><span class="line">	/** 发送邮件的服务器ip */</span><br><span class="line">	private String mailServerHost;</span><br><span class="line">	/** 发送邮件的服务器端口 */</span><br><span class="line">	private String mailServerPort = &quot;25&quot;;</span><br><span class="line">	/** 邮件发送者的地址 */</span><br><span class="line">	private String fromAddress;</span><br><span class="line">	/** 邮件接收者的地址 */</span><br><span class="line">	private String toAddress;</span><br><span class="line">	/** 登录邮件发送服务器的用户名 */</span><br><span class="line">	private String username;</span><br><span class="line">	/** 登录邮件发送服务器的密码 --授权密码*/</span><br><span class="line">	private String password;</span><br><span class="line">	/** 是否需要身份验证 */</span><br><span class="line">	private boolean isValidate = false;</span><br><span class="line">	/** 邮件主题 */</span><br><span class="line">	private String subject;</span><br><span class="line">	/** 邮件文本内容 */</span><br><span class="line">	private String content;</span><br><span class="line">	/** 邮件附件的文件 */</span><br><span class="line">	private List&lt;File&gt; fileList;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 获得邮件会话属性</span><br><span class="line">	 * @throws GeneralSecurityException  证书安全异常</span><br><span class="line">	 */</span><br><span class="line">	public Properties getProperties() throws GeneralSecurityException &#123;</span><br><span class="line">		Properties p = new Properties();</span><br><span class="line">		p.put(&quot;mail.smtp.host&quot;, this.mailServerHost);</span><br><span class="line">		p.put(&quot;mail.smtp.port&quot;, this.mailServerPort);</span><br><span class="line">		p.put(&quot;mail.smtp.auth&quot;, isValidate ? &quot;true&quot; : &quot;false&quot;);</span><br><span class="line">		// 进行证书验证</span><br><span class="line">		MailSSLSocketFactory mssf = new MailSSLSocketFactory();</span><br><span class="line">		mssf.setTrustAllHosts(true);</span><br><span class="line">		p.put(&quot;mail.smtp.ssl.enable&quot;, true);</span><br><span class="line">		p.put(&quot;mail.smtp.ssl.socketFactory&quot;, mssf);</span><br><span class="line">		return p;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getMailServerHost() &#123;</span><br><span class="line">		return mailServerHost;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setMailServerHost(String mailServerHost) &#123;</span><br><span class="line">		this.mailServerHost = mailServerHost;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getMailServerPort() &#123;</span><br><span class="line">		return mailServerPort;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setMailServerPort(String mailServerPort) &#123;</span><br><span class="line">		this.mailServerPort = mailServerPort;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getFromAddress() &#123;</span><br><span class="line">		return fromAddress;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setFromAddress(String fromAddress) &#123;</span><br><span class="line">		this.fromAddress = fromAddress;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getToAddress() &#123;</span><br><span class="line">		return toAddress;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setToAddress(String toAddress) &#123;</span><br><span class="line">		this.toAddress = toAddress;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getUsername() &#123;</span><br><span class="line">		return username;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setUsername(String username) &#123;</span><br><span class="line">		this.username = username;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getPassword() &#123;</span><br><span class="line">		return password;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setPassword(String password) &#123;</span><br><span class="line">		this.password = password;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public boolean isValidate() &#123;</span><br><span class="line">		return isValidate;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setValidate(boolean isValidate) &#123;</span><br><span class="line">		this.isValidate = isValidate;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getSubject() &#123;</span><br><span class="line">		return subject;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setSubject(String subject) &#123;</span><br><span class="line">		this.subject = subject;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getContent() &#123;</span><br><span class="line">		return content;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setContent(String content) &#123;</span><br><span class="line">		this.content = content;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public List&lt;File&gt; getFileList() &#123;</span><br><span class="line">		return fileList;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setFileList(List&lt;File&gt; fileList) &#123;</span><br><span class="line">		this.fileList = fileList;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>密码验证管理</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 2. 简单的验证管理</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class SimpleAuthenticator extends Authenticator&#123;</span><br><span class="line">	/**用户名*/</span><br><span class="line">	private String username = null;</span><br><span class="line">	/**密码*/</span><br><span class="line">	private String password = null;</span><br><span class="line"></span><br><span class="line">	public SimpleAuthenticator(String username, String password) &#123;</span><br><span class="line">		this.username = username;</span><br><span class="line">		this.password = password;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	protected PasswordAuthentication getPasswordAuthentication() &#123;</span><br><span class="line">		return new PasswordAuthentication(username, password);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>实现简单邮件服务器</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 3. 简单邮件</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class SimpleMailServer &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 以文本格式发送邮件</span><br><span class="line">	 * @param mailInfo 待发送的邮件信息</span><br><span class="line">	 * @return true 发送成功; false 发送不成功</span><br><span class="line">	 */</span><br><span class="line">	public boolean sendTextMail(MailSenderInfo mailInfo) &#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			// 判断是否需要身份验证</span><br><span class="line">			SimpleAuthenticator authenticator = null;</span><br><span class="line">			Properties properties = mailInfo.getProperties();</span><br><span class="line">			if (mailInfo.isValidate()) &#123;</span><br><span class="line">				// 如果需要身份验证， 则创建一个密码验证器</span><br><span class="line">				authenticator = new SimpleAuthenticator(mailInfo.getUsername(), mailInfo.getPassword());</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			// 根据邮件会话属性和密码验证器构造一个发送邮件的Session</span><br><span class="line">			Session sendMailSession = Session.getDefaultInstance(properties, authenticator);</span><br><span class="line"></span><br><span class="line">			// 根据Session创建一个邮件信息</span><br><span class="line">			Message mailMessage = new MimeMessage(sendMailSession);</span><br><span class="line">			// 创建邮件发送者地址</span><br><span class="line">			Address from = new InternetAddress(mailInfo.getFromAddress());</span><br><span class="line">			// 设置邮件消息的发送者</span><br><span class="line">			mailMessage.setFrom(from);</span><br><span class="line">			// 创建邮件的接收者地址，并设置到邮件消息中</span><br><span class="line">			Address to = new InternetAddress(mailInfo.getToAddress());</span><br><span class="line">			mailMessage.setRecipient(Message.RecipientType.TO, to);</span><br><span class="line">			// 设置邮件消息的主题</span><br><span class="line">			mailMessage.setSubject(mailInfo.getSubject());</span><br><span class="line">			// 设置邮件消息发送时间</span><br><span class="line">			mailMessage.setSentDate(new Date());</span><br><span class="line">			// 设置邮件消息的主要内容</span><br><span class="line">			String mailContent = mailInfo.getContent();</span><br><span class="line">			mailMessage.setText(mailContent);</span><br><span class="line">			// 发送邮件</span><br><span class="line">			Transport.send(mailMessage);</span><br><span class="line">			return true;</span><br><span class="line">		&#125; catch (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">		return false;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 以HTML格式发送邮件</span><br><span class="line">	 * </span><br><span class="line">	 * @param mailInfo</span><br><span class="line">	 *            待发送的邮件</span><br><span class="line">	 * @return true 发送成功; false 发送失败。</span><br><span class="line">	 */</span><br><span class="line">	public boolean sendHtmlMail(MailSenderInfo mailInfo) &#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			// 判断是否需要身份认证</span><br><span class="line">			SimpleAuthenticator authenticator = null;</span><br><span class="line">			Properties properties = mailInfo.getProperties();</span><br><span class="line">			// 如果需要身份认证， 则创建一个密码验证器</span><br><span class="line">			if (mailInfo.isValidate()) &#123;</span><br><span class="line">				authenticator = new SimpleAuthenticator(mailInfo.getUsername(), mailInfo.getPassword());</span><br><span class="line">			&#125;</span><br><span class="line">			// 根据邮件会话属性和密码验证器构造一个发送邮件的Session</span><br><span class="line">			Session sendMailSession = Session.getDefaultInstance(properties, authenticator);</span><br><span class="line"></span><br><span class="line">			// 根据Session创建一个邮件消息</span><br><span class="line">			Message mailMessage = new MimeMessage(sendMailSession);</span><br><span class="line">			// 创建邮件发送者地址</span><br><span class="line">			Address from = new InternetAddress(mailInfo.getFromAddress());</span><br><span class="line">			// 设置邮件消息的发送者</span><br><span class="line">			mailMessage.setFrom(from);</span><br><span class="line">			// 创建邮件的接收者地址， 并设置到邮件消息中</span><br><span class="line">			Address to = new InternetAddress(mailInfo.getToAddress());</span><br><span class="line">			// Message.RecipientType.TO属性表示接收者的类型为TO</span><br><span class="line">			mailMessage.setRecipient(Message.RecipientType.TO, to);</span><br><span class="line">			// 设置邮件消息的主题</span><br><span class="line">			mailMessage.setSubject(mailInfo.getSubject());</span><br><span class="line">			// 设置邮件消息发送的时间</span><br><span class="line">			mailMessage.setSentDate(new Date());</span><br><span class="line">			// MiniMultipart类是一个容器，包含MimeBodyPart类型的对象</span><br><span class="line">			Multipart mainPart = new MimeMultipart();</span><br><span class="line">			</span><br><span class="line">			if (null != mailInfo.getContent()) &#123;</span><br><span class="line">				setContent(mailInfo, mainPart);</span><br><span class="line">			&#125;</span><br><span class="line">			if (null != mailInfo.getFileList() &amp;&amp; mailInfo.getFileList().size() &gt; 0) &#123;</span><br><span class="line">				addAttachment(mailInfo.getFileList(), mainPart);</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			// 将MiniMultipart对象设置为邮件内容</span><br><span class="line">			mailMessage.setContent(mainPart);</span><br><span class="line">			// 发送邮件</span><br><span class="line">			Transport.send(mailMessage);</span><br><span class="line">			return true;</span><br><span class="line">		&#125; catch (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">		return false;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 设置内容</span><br><span class="line">	 * @param mailInfo 邮件信息</span><br><span class="line">	 * @param mainPart 容器</span><br><span class="line">	 * @throws MessagingException 消息异常</span><br><span class="line">	 */</span><br><span class="line">	public void setContent(MailSenderInfo mailInfo, Multipart mainPart) throws MessagingException &#123;</span><br><span class="line">		// 创建一个包含HTML内容的MimeBodyPart</span><br><span class="line">		BodyPart textContent = new MimeBodyPart();</span><br><span class="line">		// 设置HTML内容</span><br><span class="line">		textContent.setContent(mailInfo.getContent(), &quot;text/html; charset=utf-8&quot;);</span><br><span class="line">		mainPart.addBodyPart(textContent);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 添加附件</span><br><span class="line">	 * @param filePath 文件路径</span><br><span class="line">	 * @param fileName 文件名</span><br><span class="line">	 * @param mainPart 容器</span><br><span class="line">	 * @throws MessagingException 消息异常</span><br><span class="line">	 */</span><br><span class="line">	public void addAttachment(List&lt;File&gt; fileList, Multipart mainPart) throws MessagingException&#123;</span><br><span class="line">		for (int i = 0; i &lt; fileList.size(); i++)&#123;</span><br><span class="line">			File file = fileList.get(i);</span><br><span class="line">			BodyPart fileContent = new MimeBodyPart();</span><br><span class="line">			DataSource source = new FileDataSource(file.getAbsolutePath());</span><br><span class="line">			fileContent.setDataHandler(new DataHandler(source));</span><br><span class="line">			fileContent.setFileName(file.getName());</span><br><span class="line">			mainPart.addBodyPart(fileContent);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>主函数使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">public class Client &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		</span><br><span class="line">		// 设置邮件</span><br><span class="line">		MailSenderInfo mailInfo = new MailSenderInfo();</span><br><span class="line">		// 设置服务器主机名</span><br><span class="line">		mailInfo.setMailServerHost(&quot;smtp.qq.com&quot;);</span><br><span class="line">		// 设置服务器端口</span><br><span class="line">		mailInfo.setMailServerPort(&quot;465&quot;);</span><br><span class="line">		// 设置密码验证</span><br><span class="line">		mailInfo.setValidate(true);</span><br><span class="line">		// 设置用户名</span><br><span class="line">		mailInfo.setUsername(&quot;1449689807@qq.com&quot;);</span><br><span class="line">		// 设置登录验证需要的密码</span><br><span class="line">		mailInfo.setPassword(&quot;****************&quot;);// 设置为自己的验证密码</span><br><span class="line">		// 设置发送者地址</span><br><span class="line">		mailInfo.setFromAddress(&quot;1449689807@qq.com&quot;);</span><br><span class="line">		// 设置接收者地址</span><br><span class="line">		mailInfo.setToAddress(&quot;1425941077@qq.com&quot;);</span><br><span class="line">		// 设置邮件标题</span><br><span class="line">		mailInfo.setSubject(&quot;邮件标题&quot;);</span><br><span class="line">		// 设置邮件内容</span><br><span class="line">		mailInfo.setContent(&quot;邮件内容&quot;);</span><br><span class="line">		// 设置邮件携带的附件</span><br><span class="line">//		List&lt;File&gt; fileList = new ArrayList&lt;&gt;();</span><br><span class="line">//		fileList.add(new File(&quot;C:\\Users\\Administrator\\Desktop\\password.png&quot;));</span><br><span class="line">//		mailInfo.setFileList(fileList);</span><br><span class="line">		</span><br><span class="line">		// 发送邮件</span><br><span class="line">		SimpleMailServer sms = new SimpleMailServer();</span><br><span class="line">//		sms.sendTextMail(mailInfo);// 发送文本格式</span><br><span class="line">		sms.sendHtmlMail(mailInfo);// 发送html格式		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>遇到的问题<br><a href="http://www.jianshu.com/p/6ca867af1db9" target="_blank" rel="noopener">查看详情</a></p>
</li>
<li><p>Android 使用Javamail发送邮件时，要将第一步中的getProperties()函数更改为如下内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 获得邮件会话属性</span><br><span class="line"> * @throws GeneralSecurityException  证书安全异常</span><br><span class="line"> */</span><br><span class="line">Properties getProperties() throws GeneralSecurityException &#123;</span><br><span class="line">	MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();</span><br><span class="line">	mc.addMailcap(&quot;text/html;; x-java-content-handler=com.sun.mail.handlers.text_html&quot;);</span><br><span class="line">	mc.addMailcap(&quot;text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml&quot;);</span><br><span class="line">	mc.addMailcap(&quot;text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain&quot;);</span><br><span class="line">	mc.addMailcap(&quot;multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed&quot;);</span><br><span class="line">	mc.addMailcap(&quot;message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822&quot;);</span><br><span class="line">	CommandMap.setDefaultCommandMap(mc);</span><br><span class="line">	Properties p = new Properties();</span><br><span class="line">	p.put(&quot;mail.smtp.host&quot;, this.mailServerHost);</span><br><span class="line">	p.put(&quot;mail.smtp.port&quot;, this.mailServerPort);</span><br><span class="line">	p.put(&quot;mail.smtp.auth&quot;, isValidate ? &quot;true&quot; : &quot;false&quot;);</span><br><span class="line">	// 进行证书验证</span><br><span class="line">	MailSSLSocketFactory mssf = new MailSSLSocketFactory();</span><br><span class="line">	mssf.setTrustAllHosts(true);</span><br><span class="line">	p.put(&quot;mail.smtp.ssl.enable&quot;, true);</span><br><span class="line">	p.put(&quot;mail.smtp.ssl.socketFactory&quot;, mssf);</span><br><span class="line">	return p;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>其中需要activation.jar，<a href="https://code.google.com/archive/p/javamail-android/" target="_blank" rel="noopener">点击下载</a>。或者Gradle直接依赖<code>compile &#39;com.sun.mail:android-mail:1.6.0&#39;</code>，使用Gradle依赖之后，之前用到的mail.jar与此处用到的activation.jar都必须要删除，否则会重复编译jar。</p>
<p><a href="http://git.oschina.net/zaitingma/codes/xyu4wa901gndtr8bmso7216" target="_blank" rel="noopener">示例代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Java-Special-Function/Java-身份证验证(IdCardVerification)/" rel="next" title="Java-身份证验证(IdCardVerification)">
                <i class="fa fa-chevron-left"></i> Java-身份证验证(IdCardVerification)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Linux/Linux-安装Git/" rel="prev" title="Linux-安装Git">
                Linux-安装Git <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
